//
//  V4SelectView.m
//  XinKaiFa55like
//
//  Created by 55like on 23/09/2017.
//  Copyright © 2017 55like lj. All rights reserved.
//

#import "V4SelectView.h"



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#import "WSButtonGroup.h"
@interface V4SelectView ()<WSButtonGroupdelegate,UITextFieldDelegate>
{
    
    UIScrollView*scrollView;
    WSButtonGroup*_sortBtnGroup;
    WSButtonGroup*_levelBtnGroup;
    WSButtonGroup*_classBtnGroup;
    WSButtonGroup*_yearBtnGroup;
    
    UITextField*_tflow;
    UITextField*_tfHeigh;
    
    
}

@end

@implementation V4SelectView
#pragma mark  开始
+(V4SelectView *)showMeWithCallBackBlock:(AllcallBlock)callBackBlock{
    V4SelectView*view=[UTILITY.currentViewController getAddValueForKey:@"V4SelectView"];
    if (!view) {
        view=[[V4SelectView alloc]initWithFrame:CGRectMake(0, kTopHeight+40, kScreenWidth, kScreenHeight-kTopHeight-40)];
        [UTILITY.currentViewController.view addSubview:view];
        [UTILITY.currentViewController setAddValue:view forKey:@"V4SelectView"];
        [view addView];
    }
    view.callBackBlock=callBackBlock;
    view.hidden=NO;
    
    [[NSNotificationCenter defaultCenter] addObserver:view selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:view selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    return view;
}

#pragma mark -  写UI
-(void)addView{
    {
        
        UIButton *btn=[[UIButton alloc]initWithFrame:self.bounds];
        [self addSubview:btn];
        btn.backgroundColor=RGBACOLOR(127, 127, 127, 0.5);
        [btn addTarget:self action:@selector(fcloseme) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [self addViewClickBlock:^(UIView *view) {
        [view endEditing:YES];
    }];
    UIView*viewcentetn=[RHMethods viewWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kTopHeight-40-40) backgroundcolor:zrgbwhiteColor superView:self];
    
    scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kTopHeight-40-40)];
    [viewcentetn addSubview:scrollView];
    {
        WSButtonGroup*btnGroup=[WSButtonGroup new];
        _sortBtnGroup=btnGroup;
        btnGroup.delegate=self;
        NSArray*arraytitle=@[@"最新",@"浏览最多"];
        for (int i=0; i<arraytitle.count; i++) {
            NSString*titlestr=arraytitle[i];
             WSSizeButton*btn1=[RHMethods buttonWithframe:CGRectMake(0, scrollView.contentHeight+11, 80, 28) backgroundColor:zrgbwhiteColor text:titlestr font:12 textColor:rgb(102,102,102) radius:4 superview:scrollView];
            btn1.layer.borderColor=rgb(235,237,246).CGColor;
            btn1.layer.borderWidth=1;
            [btn1 setTitleColor:rgb(220,169,32) forState:UIControlStateSelected];
            [btnGroup addButton:btn1];
            if (i==0) {
                btn1.frameX=20;
            }else if(i==1){
                [btn1 beCX];
            }else{
                btn1.frameRX=20;
            }
        }
        scrollView.contentHeight=scrollView.contentHeight+11+28;
    }
    {
        UILabel*lbtitle=[RHMethods lableX:20 Y:scrollView.contentHeight+15 W:0 Height:12 font:12 superview:scrollView withColor:rgb(153,153,153) text:@"等级"];
        scrollView.contentHeight=lbtitle.frameYH;
        
    }
    {
        WSButtonGroup*btnGroup=[WSButtonGroup new];
        _levelBtnGroup=btnGroup;
        btnGroup.delegate=self;
        NSArray*arraytitle=[UTILITY.userTypeData objectForKey:@"level"];
        for (int i=0; i<arraytitle.count; i++) {
            NSDictionary*data=arraytitle[i];
            
            NSString*titlestr=[data objectForKey:@"name"];
            WSSizeButton*btn1=[RHMethods buttonWithframe:CGRectMake(0, scrollView.contentHeight+11, 80, 28) backgroundColor:zrgbwhiteColor text:titlestr font:12 textColor:rgb(102,102,102) radius:4 superview:scrollView];
            [btn1 setAddValue:data forKey:@"data"];
            btn1.layer.borderColor=rgb(235,237,246).CGColor;
            btn1.layer.borderWidth=1;
            [btn1 setTitleColor:rgb(220,169,32) forState:UIControlStateSelected];
            [btnGroup addButton:btn1];
            if (i==0) {
                btn1.frameX=20;
            }else if(i==1){
                [btn1 beCX];
            }else{
                btn1.frameRX=20;
            }
        }
        scrollView.contentHeight=scrollView.contentHeight+11+28;
    }
    {
        UILabel*lbtitle=[RHMethods lableX:20 Y:scrollView.contentHeight+15 W:0 Height:12 font:12 superview:scrollView withColor:rgb(153,153,153) text:@"类型"];
        scrollView.contentHeight=lbtitle.frameYH;
        
    }
    {
        WSButtonGroup*btnGroup=[WSButtonGroup new];
        _classBtnGroup=btnGroup;
        btnGroup.delegate=self;
        NSArray*arraytitle=[UTILITY.userTypeData objectForKey:@"types"];
        for (int i=0; i<arraytitle.count; i++) {
            NSDictionary*data=arraytitle[i];
            
            NSString*titlestr=[data objectForKey:@"name"];
            WSSizeButton*btn1=[RHMethods buttonWithframe:CGRectMake(0, scrollView.contentHeight+11, 80, 28) backgroundColor:zrgbwhiteColor text:titlestr font:12 textColor:rgb(102,102,102) radius:4 superview:scrollView];
            [btn1 setAddValue:data forKey:@"data"];
            btn1.layer.borderColor=rgb(235,237,246).CGColor;
            btn1.layer.borderWidth=1;
            [btn1 setTitleColor:rgb(220,169,32) forState:UIControlStateSelected];
            [btnGroup addButton:btn1];
            if (i==0) {
                btn1.frameX=20;
            }else if(i==1){
                [btn1 beCX];
            }else{
                btn1.frameRX=20;
            }
        }
        scrollView.contentHeight=scrollView.contentHeight+11+28;
    }
    
    {
        UILabel*lbtitle=[RHMethods lableX:20 Y:scrollView.contentHeight+15 W:0 Height:12 font:12 superview:scrollView withColor:rgb(153,153,153) text:@"注册资本"];
        scrollView.contentHeight=lbtitle.frameYH;
        
    }
    {
        NSArray*arraytitle=@[@"最低",@"最高"];
        for (int i=0; i<arraytitle.count; i++) {
            NSString*titlestr=arraytitle[i];
        UITextField*tf1=[RHMethods textFieldlWithFrame:CGRectMake(20, scrollView.contentHeight+11, 125, 28) font:Font(12) color:nil placeholder:titlestr text:@""];
            [scrollView addSubview:tf1];
            tf1.delegate=self;
            tf1.keyboardType=UIKeyboardTypeNumberPad;
            
            tf1.layer.borderColor=rgb(235,237,246).CGColor;
            tf1.layer.borderWidth=1;
            tf1.textAlignment=NSTextAlignmentCenter;
            if (i==0) {
                tf1.frameX=20;
                _tflow=tf1;
                UIView*viewline=[RHMethods viewWithFrame:CGRectMake(0, 0, 15, 1) backgroundcolor:rgbLineColor superView:scrollView];
                [viewline beCX];
                viewline.centerY=tf1.centerY;
            }else if(i==1){
                _tfHeigh=tf1;
                tf1.frameRX=20;
            }
            
        }
        scrollView.contentHeight=scrollView.contentHeight+11+28;
    }
    {
        UILabel*lbtitle=[RHMethods lableX:20 Y:scrollView.contentHeight+15 W:0 Height:12 font:12 superview:scrollView withColor:rgb(153,153,153) text:@"成立年限"];
        scrollView.contentHeight=lbtitle.frameYH;
        
    }
    {
        WSButtonGroup*btnGroup=[WSButtonGroup new];
        _yearBtnGroup=btnGroup;
        btnGroup.delegate=self;
        NSArray*arraytitle=@[@"1-2年",@"2-3年",@"3年以上"];
        for (int i=0; i<arraytitle.count; i++) {
            
            NSString*titlestr=arraytitle[i];
            WSSizeButton*btn1=[RHMethods buttonWithframe:CGRectMake(0, scrollView.contentHeight+11, 80, 28) backgroundColor:zrgbwhiteColor text:titlestr font:12 textColor:rgb(102,102,102) radius:4 superview:scrollView];
            btn1.layer.borderColor=rgb(235,237,246).CGColor;
            btn1.layer.borderWidth=1;
            [btn1 setTitleColor:rgb(220,169,32) forState:UIControlStateSelected];
            [btnGroup addButton:btn1];
            if (i==0) {
                btn1.frameX=20;
            }else if(i==1){
                [btn1 beCX];
            }else{
                btn1.frameRX=20;
            }
        }
        scrollView.contentHeight=scrollView.contentHeight+11+28;
    }
    scrollView.contentHeight=scrollView.contentHeight+23;
    scrollView.frameHeight=scrollView.contentHeight-1;
    viewcentetn.frameHeight=scrollView.frameHeight+40;
    {
        WSSizeButton*btncz=[RHMethods buttonWithframe:CGRectMake(0, viewcentetn.frameHeight-40, kScreenWidth/2, 40) backgroundColor:rgb(233,233,233) text:@"重置" font:14 textColor:rgb(51, 51, 51) radius:0 superview:viewcentetn];
        [btncz addTarget:self action:@selector(congzhiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    {
        WSSizeButton*btncz=[RHMethods buttonWithframe:CGRectMake(kScreenWidth/2, viewcentetn.frameHeight-40, kScreenWidth/2, 40) backgroundColor:rgb(220,169,32) text:@"完成" font:14 textColor:zrgbwhiteColor radius:0 superview:viewcentetn];
        
        [btncz addTarget:self action:@selector(OkBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    
}


#pragma mark - 请求数据
-(void)loadDATA{

    
}
#pragma mark - 事件监听
-(void)congzhiBtnClick:(UIButton*)btn{
    _sortBtnGroup.currentSelectBtn.layer.borderColor=rgb(235,237,246).CGColor;
    [_sortBtnGroup reset];
    
    _levelBtnGroup.currentSelectBtn.layer.borderColor=rgb(235,237,246).CGColor;
    [_levelBtnGroup reset];
    
    _classBtnGroup.currentSelectBtn.layer.borderColor=rgb(235,237,246).CGColor;
    [_classBtnGroup reset];
    
    _yearBtnGroup.currentSelectBtn.layer.borderColor=rgb(235,237,246).CGColor;
    [_yearBtnGroup reset];
    _tflow.text=@"";
    _tfHeigh.text=@"";
    
    
}
-(void)OkBtnClick:(UIButton*)btn{
    NSMutableDictionary*mdic=[NSMutableDictionary new];
    
    if (_sortBtnGroup.currentSelectBtn) {
        [mdic setObject:_sortBtnGroup.currentindex?@"hit":@"new"  forKey:@"order"];
    }
    if (_levelBtnGroup.currentSelectBtn) {
        NSDictionary*dic=[_levelBtnGroup.currentSelectBtn getAddValueForKey:@"data"];
        [mdic setObject:[dic objectForJSONKey:@"id"] forKey:@"level"];
    }
    if (_classBtnGroup.currentSelectBtn) {
        NSDictionary*dic=[_classBtnGroup.currentSelectBtn getAddValueForKey:@"data"];
        [mdic setObject:[dic objectForJSONKey:@"id"] forKey:@"typeid"];
    }
    
    
    if (_yearBtnGroup.currentSelectBtn) {
        if (_yearBtnGroup.currentindex==0) {
            [mdic setObject:@"1" forKey:@"time"];
        }else if (_yearBtnGroup.currentindex==1) {
            [mdic setObject:@"2" forKey:@"time"];
        }else if (_yearBtnGroup.currentindex==2) {
            [mdic setObject:@"3" forKey:@"time"];
        }
    }
    
    if (self.callBackBlock) {
        self.callBackBlock(mdic,200,nil);
        [self closeme];
    }
    
}

-(void)fcloseme{
    if (self.callBackBlock) {
        self.callBackBlock(nil,201,nil);
    }
    
    [self closeme];
}
-(void)closeme{
    self.hidden=YES;
    
     [[NSNotificationCenter defaultCenter]removeObserver:self];
}
#pragma mark - 代理事件
-(void)WSButtonGroupClick:(WSButtonGroup *)btnCrop{
    for (UIButton*btn in btnCrop.buttonArray) {
        btn.layer.borderColor=rgb(235,237,246).CGColor;
    }
    btnCrop.currentSelectBtn.layer.borderColor=rgb(220,169,32).CGColor;
[self endEditing:YES];

}
#pragma mark - keyboak


-(void)handleKeyboardDidShow:(NSNotification *)notification{
    
    NSDictionary *info = [notification userInfo];
    CGRect keyboardFrame;
    [[info objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
    CGFloat distanceToMove = kbSize.height;
    NSLog(@"---->动态键盘高度:%f",distanceToMove);
#pragma mark zxh UI布局 根据键盘布局
    
    if (![self getAddValueForKey:@"scrollViewH"]) {
        [self setAddValue:[NSString stringWithFormat:@"%f",scrollView.frameHeight] forKey:@"scrollViewH"];
    }
    
    scrollView.frame=CGRectMake(X(scrollView), Y(scrollView), W(scrollView),self.frame.size.height- Y(scrollView)-distanceToMove);
    
    [WSKeyBoardTool TFscrollview:scrollView];
    
}
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
    
    [UIView animateWithDuration:0.3 animations:^{
        scrollView.frame=CGRectMake(0,  Y(scrollView), kScreenWidth,[[self getAddValueForKey:@"scrollViewH"] floatValue]);
    }];
}//
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

#pragma mark - textfielddelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self endEditing:YES];
    return YES;
}

#pragma mark - UITextView Delegate Methods
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
#pragma mark  单击后键盘消失

@end

